﻿using BacklogParser.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace BacklogParser.Parsers
{
    public static class DatesRightsEffectiveParser
    {
        public static DatesRightsEffective Parse(XElement element)
        {
            var result = new DatesRightsEffective();
            var firstExaminationReport = element.Descendants(Constants.Epo + "first-examination-report-despatched").FirstOrDefault();
            if (firstExaminationReport != null)
            {
                result.FirstExaminationReportDespatched = firstExaminationReport.Descendants(Constants.Epo + "date").First().Value;
            }

            var patentMaintained = element.Descendants(Constants.Epo + "patent-maintained-as-amended").FirstOrDefault();
            if (patentMaintained != null)
            {
                result.PatentMaintainedAsAmended = patentMaintained.Descendants(Constants.Epo + "date").First().Value;
            }

            var processingInterrupted = element.Descendants(Constants.Epo + "proceedings-interrupted").FirstOrDefault();
            if (processingInterrupted != null)
            {
                result.ProceedingsInterrupted = processingInterrupted.Descendants(Constants.Epo + "date").First().Value;
            }

            var processingResumed = element.Descendants(Constants.Epo + "proceedings-resumed").FirstOrDefault();
            if (processingResumed != null)
            {
                result.ProceedingsResumed = processingResumed.Descendants(Constants.Epo + "date").First().Value;
            }

            var processingSuspended = element.Descendants(Constants.Epo + "proceedings-suspended").FirstOrDefault();
            if (processingSuspended != null)
            {
                result.ProceedingsResumed = processingSuspended.Descendants(Constants.Epo + "date").First().Value;
            }

            var requestForConversion = element.Descendants(Constants.Epo + "request-for-conversion-to-national-application").FirstOrDefault();
            if (requestForConversion != null)
            {
                result.RequestForConversionToNationalApplication_Date = requestForConversion.Descendants(Constants.Epo + "date").First().Value;
                result.RequestForConversionToNationalApplication_Country = requestForConversion.Descendants(Constants.Epo + "country").First().Value;
            }

            var requestForExamination = element.Descendants(Constants.Epo + "request-for-examination").FirstOrDefault();
            if (requestForExamination != null)
            {
                result.RequestForExamination = requestForExamination.Descendants(Constants.Epo + "date").First().Value;
            }

            return result;
        }
    }
}
